<template>
    <div>
        <!--面包屑导航-->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/teahome' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>我布置的作业</el-breadcrumb-item>
        </el-breadcrumb>
        <!--作业列表主部分-->
        <el-card>
            <el-row :gutter="25">
                <el-col span="10">
                <!--搜索区--> 
                   <el-input placeholder="输入搜索作业名" v-model="queryInfo.query" clearable @clear="getWorkList">
                    <el-button slot="append" icon="el-icon-search" @click="getWorkList"></el-button>
                   </el-input>
                </el-col>
            <!--添加区-->
                <el-col span="10">
                 <el-button type="primary" @click="addDialogVisible=true">布置作业</el-button>
                 
                </el-col>
            </el-row>
            <!--表格 border边框 stripe隔行变色-->
                <el-table :data="workList" border stripe>
                <el-table-column label="班级" prop="cid"></el-table-column>
                <el-table-column label="标题" prop="wtitle"></el-table-column>
                <el-table-column label="内容" prop="wcontent"></el-table-column>
                <el-table-column label="图片" prop="bpicture"></el-table-column>
                <el-table-column label="文书id" prop="bid"></el-table-column>
                <el-table-column label="开始时间" prop="wbtime"></el-table-column>
                <el-table-column label="截止时间" prop="wetime" ></el-table-column>
                <el-table-column label="操作" prop="Action">
           
               <template slot-scope="scope">
               <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row)">修改</el-button>
               <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteWork(scope.row)">删除</el-button>
           </template>
        </el-table-column>
            </el-table>
            <!--分页组件-->
            <div>
                <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="currentPage4"
                    :page-sizes="[5, 10, 20, 30]"
                    :page-size="queryInfo.pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
                </el-pagination>
            </div>
        </el-card>
        <!--添加区域弹窗-->
        <el-dialog wtitle="发布作业" :visible.sync="addDialogVisible" wwidth="50%" @close="addDialogClose">
            <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-wwidth="70px">
                <el-form-item label="作业id" prop="wid">
                    <el-input v-model="addForm.wid"></el-input>
                </el-form-item>
                <el-form-item label="班级" prop="cid">
                    <el-input v-model="addForm.cid"></el-input>
                </el-form-item>
                <el-form-item label="标题" prop="wtitle">
                    <el-input v-model="addForm.wtitle"></el-input>
                </el-form-item>
                <el-form-item label="内容" prop="wcontent">
                    <el-input v-model="addForm.wcontent"></el-input>
                </el-form-item>
                <el-form-item label="文书id" prop="bid">
                    <el-input v-model="addForm.bid"></el-input>
                </el-form-item>
                <el-form-item label="开始时间" prop="wbtime">
                    <el-date-picker v-model="addForm.wbtime" type="date"  placeholder="选择日期"></el-date-picker>
                 
                </el-form-item>
                <el-form-item label="结束时间" prop="wetime">
                    <el-date-picker v-model="addForm.wetime" type="date"  placeholder="选择日期"></el-date-picker>
               
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">  
                <el-button @click="addDialogVisible=false">取消</el-button>
                <el-button type="primary" @click="addWork">发布</el-button>
            </span>
        </el-dialog>
        <!--修改区域弹窗-->
        <el-dialog title="修改作业信息" :visible.sync="editDialogVisible" width="50%" @close="editDialogClose">
            <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="70px">
                <el-form-item label="作业标题" prop="wtitle">
                    <el-input v-model="editForm.wtitle"></el-input>
                </el-form-item>
                <el-form-item label="作业内容" prop="wcontent">
                    <el-input v-model="editForm.wcontent"></el-input>
                </el-form-item>
                <el-form-item label="文书id" prop="bid">
                    <el-input v-model="editForm.bid"></el-input>
                </el-form-item>
                <el-form-item label="开始时间" prop="wbtime">
                    <el-date-picker v-model="editForm.wbtime" type="date"  placeholder="选择日期"></el-date-picker>
                 
                </el-form-item>
                <el-form-item label="结束时间" prop="wetime">
                    <el-date-picker v-model="editForm.wetime" type="date"  placeholder="选择日期"></el-date-picker>
               
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">  
                <el-button @click="editDialogVisible=false">取消</el-button>
                <el-button type="primary" @click="editClass">确认修改</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script>
    export default{
        created(){
           this.getWorkList();
        },
        data(){
            return{
                //查询信息实体
                queryInfo:{
                    query:"",
                    pageNum:1,
                    pageSize:4,
                    nowtname:this.$userMsg.tname,
                    nowtpwd:this.$userMsg.tpwd,
                },
                workList:[],//学生列表
                total:0,
                //对话框状态
                addDialogVisible:false,
                editDialogVisible:false,
                //表单信息
                addForm:{
                    wid:"",
                    cid:"",
                    bid:"",
                    wtitle:"",
                    wcontent:"",
                    wbtime:"",
                    wetime:"",
                },
                editForm:{
                    cid:"",
                    bid:"",
                    wtitle:"",
                    wcontent:"",
                    wbtime:"",
                    wetime:"",
                },
                //表单验证
                //表单验证
                addFormRules:{
                    wid: [
                        { required: true, message: '请输入作业wid', trigger: 'blur' },
                       
                          ],
                    wtitle: [
                        { required: true, message: '请输入作业标题', trigger: 'blur' },
                      
                          ],   
                    wcontent: [
                        { required: true, message: '请输入作业内容', trigger: 'blur' },
               
                          ],      
                    wetime: [
                        { required: true, message: '请输入作业开始时间', trigger: 'blur' },
               
                          ],   
                    wbtime: [
                        { required: true, message: '请输入作业结束时间', trigger: 'blur' },
            
                          ],
                     },
                 editFormRules:{
                    wtitle: [
                        { required: true, message: '请输入作业标题', trigger: 'blur' },
                      
                          ],   
                    wcontent: [
                        { required: true, message: '请输入作业内容', trigger: 'blur' },
               
                          ],      
                    wetime: [
                        { required: true, message: '请输入作业开始时间', trigger: 'blur' },
               
                          ],   
                    wbtime: [
                        { required: true, message: '请输入作业结束时间', trigger: 'blur' },
            
                          ],
                     },
            }

        },
        methods:{
           async getWorkList(){
                const {data:res} =await this.$http.get("allWork",{params:this.queryInfo});
                this.workList=res.data;//列表数据封装
                this.total=res.numbers;//总数封装
            },
            //最大数
            handleSizeChange(newSize){
                this.queryInfo.pageSize=newSize;
                this.getWorkList();
            },
            //pageNum的触发
            handleCurrentChange(newPage){
                this.queryInfo.pageNum=newPage;
                this.getWorkList();
            },
            //添加
            //监听添加操作
            addDialogClose(){
                this.$refs.addFormRef.resetFields();
            },
            addWork(){
                this.$refs.addFormRef.validate(async valid=>{
                   if(!valid) return;

                   const {data:res}=await this.$http.post("addWork",this.addForm);
                   if(res!="success"){
                       return this.$message.error("发布失败！");
                   }
                   this.$message.success("发布成功！");
                   this.addDialogVisible=false;
                   this.getWorkList();
                    
                })
            },
            //删除
            async deleteWork(s){
               const confirmResult=await this.$confirm('此操作将永久删除，是否继续','提示',{
                    confirmButtonText:'确定',
                    cancleButtonText:'取消',
                    type:'warning'
                }).catch(err=>err)
                if(confirmResult!='confirm'){
                    return this.$message.Info('已取消删除');
                }
                const {data:res}=await this.$http.post("deleteWork",s);
                if(res!="success"){
                    return this.$message.error("删除失败！");
                }
                this.$message.success("删除成功！");
                this.getWorkList();
            },
            //修改
            //显示对话框，按钮触发
            async showEditDialog(s){
               // const {data:res}=await this.$http.get("editClass"+Id);
                this.editForm=s;//存储查询信息
                this.editDialogVisible=true;//开启编辑框
            },
            //监听修改操作
            editDialogClose(){
                this.$refs.editFormRef.resetFields();//关闭窗口，重置信息
            },
            //确认修改
            editClass(){
                this.$refs.editFormRef.validate(async valid=>{
                    if(!valid) return;
                  const {data:res}=await this.$http.post("editClass",this.editForm);
                   if(res!="success"){
                       return this.$message.error("修改失败！");
                   }else{
                   this.$message.success("修改成功！");
                   this.editDialogVisible=false;
                   this.getWorkList();
                   }
            
                });
            },
        }

    }
</script>
<style  scoped>
.el-breadcrumb{
    margin-bottom: 15px;
    font-size: 15px;
}
</style>